package com.blb.article_ms.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.article_ms.entity.ResponseResult;
import com.blb.article_ms.entity.dto.ArticleDTO;
import com.blb.article_ms.entity.po.ArticlePO;
import com.blb.article_ms.entity.vo.ArticleVO;
import com.blb.article_ms.service.IArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xray
 * @since 2023-06-14
 */
@Api("文章控制器")
@CrossOrigin
@RestController
public class ArticleController {

    @Autowired
    private IArticleService articleService;

    @ApiOperation("文章分页")
    @GetMapping("/article-page")
    public ResponseResult<Page<ArticleVO>> getArticlePage(@RequestParam(required = false,defaultValue = "1") @ApiParam("当前页数") Long current,
                                                          @RequestParam(required = false,defaultValue = "2") @ApiParam("每页行数") Long size,
                                                          @RequestParam(required = false,defaultValue = "") @ApiParam("标题") String title,
                                                          @RequestParam(required = false,defaultValue = "") @ApiParam("内容") String content){
        Page<ArticleVO> page = articleService.getArticlePage(current, size, title, content);
        return ResponseResult.ok(page);
    }

    @ApiOperation("文章添加")
    @PostMapping("/article")
    public ResponseResult<String> addArticle(@RequestBody @Validated ArticleDTO articleDTO){
        ArticlePO articlePO = new ArticlePO();
        //将dto对象的属性复制给po对象
        BeanUtils.copyProperties(articleDTO,articlePO);
        articleService.save(articlePO);
        return ResponseResult.ok("ok");
    }

    @ApiOperation("文章更新")
    @PutMapping("/article")
    public ResponseResult<String> updateArticle(@RequestBody @Validated ArticleDTO articleDTO){
        ArticlePO articlePO = new ArticlePO();
        //将dto对象的属性复制给po对象
        BeanUtils.copyProperties(articleDTO,articlePO);
        return ResponseResult.ok("ok");
    }

    @ApiOperation("文章删除")
    @DeleteMapping("/article/{id}")
    public ResponseResult<String> updateArticle(@PathVariable Long id){
        articleService.removeArticle(id);
        return ResponseResult.ok("ok");
    }
}
